package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysUserDto;
import com.itheima.ydd.dto.UserNumDto;
import com.itheima.ydd.entity.*;
import com.itheima.ydd.mapper.SysUserMapper;
import com.itheima.ydd.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.vo.BaseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements ISysUserService {
    @Autowired
    private ISysUserService sysUserService; //

    @Autowired
    private ISysRoleService sysRoleService; //角色

    @Autowired
    private ISysMenuService sysMenuService; //菜单

    @Autowired
    private ISysDictService sysDictService; //

    @Autowired
    private IYddCateService yddCateService; // 分类管理

    @Autowired
    private IYddArticleService yddArticleService; // 文章

    @Autowired
    private IYddAdvposService yddadvposService; // 广告位

    @Autowired
    private IYddTagService yddTagService; // 标签

    @Autowired
    private IYddAdvimgService yddAdvimgService ; //广告图

    @Autowired
    private IYddUserService yddUserService; // 用户


    /**
     *
     * @param userId
     * @return
     */
    @Override
    public R profile(Long userId) {
        //1.1 查询登录用户信息
        SysUser sysUser = sysUserService.getById(userId);
        //1.2 填入用户角色信息
        LambdaQueryWrapper<SysRole> sysRoleWrapper = new LambdaQueryWrapper<>();
        String sysUserRoleId = sysUser.getRoleId();
        sysRoleWrapper.eq(SysRole::getId,sysUserRoleId.substring(2, sysUserRoleId.length() - 2));
//        sysRoleWrapper.select(SysRole::getName);
        //1.2.1 查出对应的角色详细信息
        SysRole sysRole = sysRoleService.getOne(sysRoleWrapper);
        //1.2.2 封装进sysUserDto对象中
        SysUserDto userInfo = new SysUserDto();
        BeanUtils.copyProperties(sysUser,userInfo);
        userInfo.setRolename(sysRole.getName());

        //2 查菜单数据
        LambdaQueryWrapper<SysMenu> sysMenuWrapper = new LambdaQueryWrapper<>();
        sysMenuWrapper.ne(SysMenu::getPath,"");
        List<SysMenu> menuList = sysMenuService.list(sysMenuWrapper);

        //3 dictsList
        LambdaQueryWrapper<SysDict> sysDictWrapper = new LambdaQueryWrapper<>();
        sysDictWrapper.select(SysDict::getType);
        sysDictWrapper.groupBy(SysDict::getType);
        //3.1 分组查询所有dict_type
        List<Object> dictTypes = sysDictService.listObjs(sysDictWrapper);
        Map<String, Map<String,String>> dictsList = new HashMap<>();
        //3.2 遍历dictTypes ，查询对应的dict信息，存入dictsList中
        for (Object o : dictTypes) {
            Map<String, String> map = new HashMap<>();

            sysDictWrapper = new LambdaQueryWrapper<>();
            sysDictWrapper.eq(SysDict::getType,o);
            List<SysDict> sysDicts = sysDictService.list(sysDictWrapper);
            for (SysDict sysDict : sysDicts) {
                //3.3 将dict_value,dict_label存入集合
                map.put(sysDict.getValue(),sysDict.getLabel());
            }

            //3.4 将保存后的数据存入dictsList
            dictsList.put((String)o,map);
        }

        //4 准备 btnList
        Map<Integer,String> btnList = new HashMap<>();
        btnList.put(0,"*");

        //5 将所有查出的数据存入 R 中，返回
        R r = new R();
        r.putData("btnList",btnList);
        r.putData("dictsList",dictsList);
        r.putData("menuList",menuList);
        r.putData("userInfo",userInfo);

        r.setErrno(0);
        r.setErrmsg("");
        return r;
    }


    /**
     * 获取前台的页面数据 ---》
     *
     * @return
     */
    @Override
    public ResultData baseInfo() {
        BaseInfoVo baseInfoVo = new BaseInfoVo();
        //所有分类数据
        List<YddCate> yddCates = yddCateService.list();
        //获取对应分类下的文章数量
        for (YddCate yddCate : yddCates) {
            LambdaQueryWrapper<YddArticle> yddWrapper = new LambdaQueryWrapper<>();
            yddWrapper.eq(YddArticle::getCateid, yddCate.getId());
            List<YddArticle> articles = yddArticleService.list(yddWrapper);
            yddCate.setNum(articles.size());
        }
        baseInfoVo.setAll_cate(yddCates);

        //获取广告位数量
        List<YddAdvpos> advposList = yddadvposService.list();
        baseInfoVo.setAdvpos_num(advposList.size());
        //标签总数
        List<YddTag> tagList = yddTagService.list();
        baseInfoVo.setTag_num(tagList.size());
        //管理员总数
        List<SysUser> userList = sysUserService.list();
        baseInfoVo.setAdmin_num(userList.size());
        //文章总数
        List<YddArticle> articleList = yddArticleService.list();
        baseInfoVo.setArticle_num(articleList.size());
        //分类总数
        baseInfoVo.setCate_num(yddCates.size());
        //广告图总数
        List<YddAdvimg> yddAdvimgList = yddAdvimgService.list();
        baseInfoVo.setAdvimg_num(yddAdvimgList.size());

        //最新用户信息
        Page page = new Page(1, 7);
        LambdaQueryWrapper<YddUser> userLqw = new LambdaQueryWrapper<>();
        userLqw.orderByDesc(YddUser::getId);
//        List<YddUser> yddUserList = yddUserService.list(userLqw);
        Page pageInfo = yddUserService.page(page, userLqw);
        List<YddUser> yddUserList = pageInfo.getRecords();
        baseInfoVo.setNew_user(yddUserList);

        //热门文章数据
        LambdaQueryWrapper<YddArticle> articleLqw = new LambdaQueryWrapper<>();
        articleLqw.orderByDesc(YddArticle::getLovenum);
        Page pages = yddArticleService.page(page, articleLqw);
        List<YddArticle> yddArticleList = pages.getRecords();
        baseInfoVo.setHot_article(yddArticleList);

        //用户数据
        LambdaQueryWrapper<YddUser> yddLqw = new LambdaQueryWrapper<>();
        List<YddUser> list = yddUserService.list(yddLqw);
        yddLqw.eq(YddUser::getSex, 1);
        List<YddUser> manList = yddUserService.list(yddLqw);
        UserNumDto userNumDto = new UserNumDto();
        userNumDto.setTotal(list.size());
        userNumDto.setUser_man(manList.size());
        userNumDto.setUser_woman(list.size() - manList.size());
        baseInfoVo.setUser(userNumDto);

        return ResultData.ok("",baseInfoVo);
    }
}
